package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage() ,dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    // @Transactional要么同时成功要么同时失败
    @Transactional
    public void add(DishDTO dishDTO) {
        //创建dish对象
        Dish dish = new  Dish();
        DishFlavor dishFlavor = new DishFlavor();
        //将dishDTO中的数据拷贝到dish对象中
        dishFlavor.setDishId(dishDTO.getId());
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.ENABLE);
        //插入菜品数据到数据库
        dishMapper.add(dish);
        Long id = dish.getId();

        if (dishDTO.getFlavors() != null && dishDTO.getFlavors().size() > 0){
            for (DishFlavor flavor : dishDTO.getFlavors()) {
                flavor.setDishId(id);
            }
        }
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }

    @Override
    public DishVO getById(Long id) {
        return dishMapper.getById(id);
    }

    @Override
    public void update(DishDTO dishDTO) {

        Dish dish = new Dish();
        //将dishDTO中的数据拷贝到dish对象中
        BeanUtils.copyProperties(dishDTO,dish);
        //更新菜品数据
        dishMapper.update(dish);
        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断flavors是否为空
        if (flavors != null && flavors.size() > 0){
            //为每个口味设置菜品id
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            //批量插入
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public void delete(List<Long> ids) {
        /*
        //TODO 1.删除菜品时判断菜品是否被套餐关联
        //TODO 2.删除菜品时判断菜品状态如果是启售状态不能删除
         */
        //批量删除菜品遍历ids集合使用循环对菜品id进行删除多次执行业务
        for (Long id : ids){
            List<Long> setmealIds = dishMapper.getSetmealIdsByDishId(id);
            if (setmealIds != null && setmealIds.size() > 0){
                throw new DeletionNotAllowedException("当前菜品被套餐关联，不能删除");
            }
            if (dishMapper.getById(id).getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException("当前菜品处于启售状态，不能删除");
            }
            else {
                dishMapper.delete(id);
                dishFlavorMapper.deleteByDishId(id);
            }
        }

    }

    @Override
    public List<DishVO> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        // 创建一个Dish对象
        // 设置要修改的字段
        Dish dish =  Dish.builder().id(id).status(status).build();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);

        if ( status == StatusConstant.DISABLE){
            //如果该菜品停售则要判断是否被套餐关联,如果关联套餐则套餐也要停售
            List<Long> setmeals= dishMapper.getSetmealIdsByDishId(id);
            if (setmeals != null && setmeals.size() > 0){
                for (Long setmealId : setmeals) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(setmealId);
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmealMapper.update(setmeal);
                }
            }

        }

    }
}
